/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseModuleDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseModuleDao : BaseDao
    {
        public BaseModuleDao()
        {
            base.CurrentTableName = BaseModuleTable.TableName;
        }

        public BaseModuleDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseModuleDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(string fullName)
        {
            string statusCode = string.Empty;
            BaseModuleEntity moduleEntity = new BaseModuleEntity
            {
                FullName = fullName
            };
            return this.Add(moduleEntity, out statusCode);
        }

        public string Add(BaseModuleEntity moduleEntity, out string statusCode)
        {
            string str = string.Empty;
            if (this.Exists(BaseModuleTable.FieldParentID, moduleEntity.ParentID, BaseModuleTable.FieldFullName, moduleEntity.FullName))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            if (this.Exists(BaseModuleTable.FieldParentID, moduleEntity.ParentID, BaseModuleTable.FieldCode, moduleEntity.Code))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }

            str = this.AddEntity(moduleEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddEntity(BaseModuleEntity moduleEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseModuleTable.TableName);
            moduleEntity.SortCode = sequence;
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseModuleTable.TableName);
            sqlBuilder.SetValue(BaseModuleTable.FieldID, sequence);
            this.SetEntity(sqlBuilder, moduleEntity);
            sqlBuilder.SetValue(BaseModuleTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseModuleTable.FieldCreateDate);
            return ((sqlBuilder.EndInsert() > 0) ? sequence : string.Empty);
        }

        public override int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseModuleEntity moduleEntity = new BaseModuleEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseModuleTable.FieldID, DataRowVersion.Original].ToString();
                    if ((id.Length > 0) && row[BaseModuleTable.FieldAllowDelete, DataRowVersion.Original].ToString().Equals("1"))
                    {
                        num += this.DeleteEntity(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseModuleTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    moduleEntity.GetFrom(row);
                    if (base.AllowEdit)
                    {
                        num += this.UpdateEntity(moduleEntity);
                    }
                }
                if (row.RowState == DataRowState.Added)
                {
                    moduleEntity.GetFrom(row);
                    num += (this.AddEntity(moduleEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            base.ReturnStatusCode = StatusCode.OK.ToString();
            return num;
        }

        public DataTable GetDTByPermission(string userID, string permissionCode)
        {
            string[] ids = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo).GetResourceScopeIDs(userID, "Module", permissionCode);
            return this.GetDT(ids);
        }

        public DataTable GetDTByUser(string userID)
        {
            string[] strArray = new string[0];
            strArray = this.GetIDs(BaseModuleTable.FieldIsPublic, "1", BaseModuleTable.FieldEnabled, "1", BaseModuleTable.FieldID);
            string[] ids = BaseBusinessLogic.FieldToArray(BaseResourcePermissionDao.Instance.GetPermission(base.DbHelper, base.UserInfo), BasePermissionTable.FieldID);
            string[] strArray3 = new string[0];
            if ((ids != null) && (ids.Length > 0))
            {
                string sqlQuery = string.Empty;
                sqlQuery = " SELECT " + BaseResourcePermissionTable.FieldResourceID + "   FROM " + BaseResourcePermissionTable.TableName + "  WHERE " + BaseResourcePermissionTable.FieldResourceCategory + " = 'Module'         AND " + BaseResourcePermissionTable.FieldPermissionID + " IN (" + BaseBusinessLogic.ObjectsToList(ids) + ")";
                DataTable dataTable = new DataTable(BaseResourcePermissionTable.TableName);
                base.DbHelper.Fill(dataTable, sqlQuery);
                strArray3 = BaseBusinessLogic.FieldToArray(dataTable, BaseResourcePermissionTable.FieldResourceID);
            }
            BaseResourcePermissionScopeDao dao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
            string permissionCode = "ModuleAccess";
            string[] strArray4 = dao.GetResourceScopeIDs(userID, "Module", permissionCode);
            string[] strArray5 = BaseBusinessLogic.Concat(new string[][] { strArray, strArray3, strArray4 });
            return this.GetDT(strArray5);
        }

        public BaseModuleEntity GetEntity(string id)
        {
            return new BaseModuleEntity(this.Get(id));
        }

        public DataTable GetRootList()
        {
            return base.GetDTByParent(string.Empty);
        }

        public int ResetSortCode(string parentID)
        {
            int num = 0;
            DataTable dTByParent = base.GetDTByParent(parentID);
            string id = string.Empty;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseModuleTable.TableName, dTByParent.Rows.Count);
            int index = 0;
            foreach (DataRow row in dTByParent.Rows)
            {
                id = row[BaseModuleTable.FieldID].ToString();
                num += this.SetProperty(id, BaseModuleTable.FieldSortCode, strArray[index]);
                index++;
            }
            return num;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseModuleEntity moduleEntity)
        {
            sqlBuilder.SetValue(BaseModuleTable.FieldParentID, moduleEntity.ParentID);
            sqlBuilder.SetValue(BaseModuleTable.FieldCode, moduleEntity.Code);
            sqlBuilder.SetValue(BaseModuleTable.FieldFullName, moduleEntity.FullName);
            sqlBuilder.SetValue(BaseModuleTable.FieldNavigateUrl, moduleEntity.NavigateUrl);
            sqlBuilder.SetValue(BaseModuleTable.FieldWinForm, moduleEntity.WinForm);
            sqlBuilder.SetValue(BaseModuleTable.FieldTarget, moduleEntity.Target);
            sqlBuilder.SetValue(BaseModuleTable.FieldDescription, moduleEntity.Description);
            sqlBuilder.SetValue(BaseModuleTable.FieldIsGroup, moduleEntity.IsGroup ? 1 : 0);
            sqlBuilder.SetValue(BaseModuleTable.FieldIsPublic, moduleEntity.IsPublic ? 1 : 0);
            sqlBuilder.SetValue(BaseModuleTable.FieldExpand, moduleEntity.Expand ? 1 : 0);
            sqlBuilder.SetValue(BaseModuleTable.FieldEnabled, moduleEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BaseModuleTable.FieldAllowEdit, moduleEntity.AllowEdit ? 1 : 0);
            sqlBuilder.SetValue(BaseModuleTable.FieldAllowDelete, moduleEntity.AllowDelete ? 1 : 0);
            sqlBuilder.SetValue(BaseModuleTable.FieldSortCode, moduleEntity.SortCode);
        }

        public int Update(BaseModuleEntity moduleEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseModuleTable.TableName, moduleEntity.ID, moduleEntity.ModifyUserID, moduleEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if ((moduleEntity.Code.Length > 0) && this.Exists(BaseModuleTable.FieldCode, moduleEntity.Code, moduleEntity.ID))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            num = this.UpdateEntity(moduleEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseModuleEntity moduleEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseModuleTable.TableName);
            this.SetEntity(sqlBuilder, moduleEntity);
            sqlBuilder.SetValue(BaseModuleTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseModuleTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseModuleTable.FieldID, moduleEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}